package mainfuncs

import (
	"fmt"

	"github.com/astaxie/beego/config"
)

//AppLogConfig 存储配置信息
var (
	AppLogConfig *LogConfig
)

//LogConfig 存储配置信息结构体
type LogConfig struct {
	KafkaAddr  string
	ESAddr     string
	LogPath    string
	LogLevel   string
	KafkaTopic string
}

//InitConfig 配置初始化，根据配置文件类型，文件名读取配置
func InitConfig(confType string, filename string) (err error) {
	conf, err := config.NewConfig(confType, filename)
	if err != nil {
		fmt.Println("new config failed, err:", err)
		return
	}

	AppLogConfig = &LogConfig{}

	AppLogConfig.LogLevel = conf.String("logs::log_level")
	if len(AppLogConfig.LogLevel) == 0 {
		AppLogConfig.LogLevel = "debug"
	}

	AppLogConfig.LogPath = conf.String("logs::log_path")
	if len(AppLogConfig.LogPath) == 0 {
		AppLogConfig.LogPath = "./logs"
	}

	AppLogConfig.KafkaAddr = conf.String("kafka::server_addr")
	if len(AppLogConfig.KafkaAddr) == 0 {
		err = fmt.Errorf("invalid kafka addr")
		return
	}

	AppLogConfig.KafkaTopic = conf.String("kafka::topic")
	if len(AppLogConfig.KafkaTopic) == 0 {
		err = fmt.Errorf("invalid kafka topic")
		return
	}

	AppLogConfig.ESAddr = conf.String("es::addr")
	if len(AppLogConfig.ESAddr) == 0 {
		err = fmt.Errorf("invalid es addr")
		return
	}

	return
}
